home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / GOFER / docsrc / pfmt / c next >
Text File  |  1994-06-06  |  7KB  |  310 lines

  1. #include <stdio.h>
  2.  
  3. /* Crude page formatter:
  4.  *
  5.  *
  6.  * .pa          start new page
  7.  * .co xxx      comment
  8.  * .ti title    set title string
  9.  * .TI title    set and print title string
  10.  * .st title    set section title
  11.  * .ST title    set and print section title
  12.  * .in file     include contents of file
  13.  * .op          page headings off
  14.  * .po          page headings on
  15.  * .pn n        set page number (numbers <=0 don't print)
  16.  * .pl n        set page length
  17.  * .ht n        blank lines above header line
  18.  * .hb n        blank lines below header line
  19.  * .ft n        blank lines above page number line
  20.  * .fb n        blank lines below page number line
  21.  * .ss n        minimum number of lines on page before new section begins
  22.  * .cc n        start new page unless at least n clear lines on page
  23.  * .mi n        only print pages numbered >= n
  24.  * .mx n        print only pages numbered <= n (if max<=min, all pages printed)
  25.  * .off         hide output
  26.  * .on          show output
  27.  * .>stdout    send following output to stdout
  28.  * .>file    send following output to named file
  29.  */
  30.  
  31. #define MAXLINE   1024
  32. #define MAXINPUT  32
  33.  
  34. int physicalLines   = 66;
  35. int headerLines1    = 2;
  36. int headerLines2    = 2;
  37. int footerLines1    = 2;
  38. int footerLines2    = 2;
  39. int suppressHeaders = 0;
  40. int contentsReqd    = 0;
  41. int lastSect        = 0;
  42. int sectSkip        = 5;
  43. int minPage         = 0;
  44. int maxPage         = 0;
  45. int outputOn        = 1;
  46.  
  47. int sofarThisPage;
  48. int linesThisPage;
  49. int pageNumber;
  50.  
  51. FILE *outputFile = 0;
  52. FILE *inputs[MAXINPUT];
  53. int  inputLevel=0;
  54. #define in inputs[inputLevel]
  55.  
  56. includeFile(n)
  57. char *n; {
  58.     if (inputLevel<MAXINPUT-1) {
  59.     inputs[++inputLevel] = fopen(n,"r");
  60.     if (!in) {
  61.         fprintf(stderr,"Cannot open file \"%s\"\n",n);
  62.         inputLevel--;
  63.     }
  64.     }
  65.     else
  66.     fprintf(stderr,"Too many included files\n");
  67. }
  68.  
  69. int readLine(s)
  70. char *s; {
  71.     int i = 0;
  72.     int c;
  73.  
  74.     while (feof(in)) {
  75.     if (inputLevel>0) {
  76.         fclose(in);
  77.         inputLevel--;
  78.     }
  79.     else
  80.         return 0;
  81.     }
  82.  
  83.     while ((c=getc(in))!=EOF && c!='\n')
  84.     if (c!=26 && c!='\r')
  85.         if (i<MAXLINE)
  86.         s[i++] = c;
  87.         else
  88.         fprintf(stderr,"input line too long\n");
  89.     s[i] = '\0';
  90.     return 1;
  91. }
  92.  
  93. char title[MAXLINE+1];
  94. char sectitle[MAXLINE+1];
  95.  
  96. setSecTitle(t)
  97. char *t; {
  98.  
  99.     strcpy(sectitle,t);
  100.  
  101.     if (contentsReqd) {
  102.         int i;
  103.         int thisSect = atoi(t);
  104.         if ((lastSect>0 && thisSect!=lastSect) || thisSect==0)
  105.         fprintf(stderr,"\n.cc 2\n");
  106.         fprintf(stderr,"    ");
  107.         for (i=0; *t; ++i)
  108.         fputc(*t++, stderr);
  109.         for (; i<66; i++)
  110.         fputc(((i&1) ? '.' : ' '), stderr);
  111.     fprintf(stderr,"%3d\n",pageNumber);
  112.     lastSect = thisSect;
  113.     }
  114.  
  115.     if (sofarThisPage + sectSkip >= linesThisPage)
  116.     clearPage();
  117. }
  118.  
  119. outputLine(s)
  120. char *s; {
  121.     if (!contentsReqd && outputOn &&
  122.         (maxPage<=minPage ||
  123.          (pageNumber>=minPage && pageNumber<=maxPage)))
  124.     if (outputFile) {
  125.         fputs(s,outputFile);
  126.         fputc('\n',outputFile);
  127.     }
  128.     else
  129.         puts(s);
  130.     sofarThisPage++;
  131. }
  132.  
  133. startPage() {
  134.     if (suppressHeaders)
  135.         linesThisPage = physicalLines;
  136.     else {
  137.     static char buffer[MAXLINE+1];
  138.         int i;
  139.         for (i=0; i<headerLines1; i++)
  140.         outputLine("");
  141.  
  142.     sprintf(buffer,"%-20s%50s",title,sectitle);
  143.         outputLine(buffer);
  144.  
  145.         for (i=0; i<headerLines2; i++)
  146.         outputLine("");
  147.     linesThisPage = physicalLines - headerLines1
  148.                                       - 1
  149.                                       - headerLines2
  150.                                       - footerLines1
  151.                       - 1
  152.                                       - footerLines2;
  153.     }
  154.     sofarThisPage = 0;
  155. }
  156.  
  157. clearPage() {
  158.     int i;
  159.     while (sofarThisPage<linesThisPage)
  160.     outputLine("");
  161.     if (!suppressHeaders) {
  162.     static char buffer[MAXLINE+1];
  163.  
  164.         for (i=0; i<footerLines1; i++)
  165.         outputLine("");
  166.  
  167.         if (pageNumber>0) {
  168.             sprintf(buffer,"%38s%d","",pageNumber);
  169.         outputLine(buffer);
  170.     }
  171.         else
  172.         outputLine("");
  173.  
  174.         for (i=0; i<footerLines2; i++)
  175.         outputLine("");
  176.     }
  177.     sofarThisPage = 0;
  178.     pageNumber++;
  179. }
  180.  
  181. startDocument() {
  182.     inputLevel      = 0;
  183.     in              = stdin;
  184.     pageNumber      = 1;
  185.     sofarThisPage   = 0;
  186.     linesThisPage   = physicalLines;
  187.     suppressHeaders = 0;
  188.     lastSect        = 0;
  189.     strcpy(title,"");
  190.     strcpy(sectitle,"");
  191. }
  192.  
  193. endDocument() {
  194.     clearPage();
  195.     if (outputFile)
  196.     fclose(outputFile);
  197.     outputFile = 0;
  198. }
  199.  
  200. int setParam(line,s,v)
  201. char *line;
  202. char *s;
  203. int  *v; {
  204.     while (*s!='\0' && *s==*line)
  205.     s++, line++;
  206.     if (*s)
  207.     return 0;
  208.     *v = atoi(line);
  209.     return 1;
  210. }
  211.  
  212. int matchesFlag(line,s)  
  213. char *line; 
  214. char *s; {
  215.     while (*s!='\0' && *s==*line) 
  216.         s++, line++;
  217.     return (*s == '\0');
  218. }
  219.  
  220. main(argc,argv)
  221. int argc;
  222. char *argv[]; {
  223.     char buffer[MAXLINE+1];
  224.     int  condClear;
  225.  
  226.     for (argc--, argv++; argc>0; argc--, argv++)
  227.     if (strcmp(*argv,"-c")==0)
  228.         contentsReqd = 1;
  229.     else
  230.         fprintf(stderr,"unknown command line argument \"%s\"\n",*argv);
  231.  
  232.     startDocument();
  233.  
  234.     while (readLine(buffer)) {
  235. ugly:
  236.     if (matchesFlag(buffer,".pa"))
  237.         clearPage();
  238.         else if (matchesFlag(buffer,".on"))
  239.         outputOn = 1;
  240.         else if (matchesFlag(buffer,".off"))
  241.         outputOn = 0;
  242.         else if (matchesFlag(buffer,".co"))
  243.         continue;
  244.         else if (matchesFlag(buffer,".ti "))
  245.             strcpy(title,buffer+4);
  246.     else if (matchesFlag(buffer,".TI ")) {
  247.         strcpy(title,buffer+4);
  248.         strcpy(buffer,title);
  249.         goto ugly;
  250.     }
  251.     else if (matchesFlag(buffer,".st "))
  252.         setSecTitle(buffer+4);
  253.     else if (matchesFlag(buffer,".ST ")) {
  254.         setSecTitle(buffer+4);
  255.         strcpy(buffer,sectitle);
  256.         goto ugly;
  257.     }
  258.     else if (matchesFlag(buffer,".>stdout")) {
  259.         if (outputFile)
  260.         fclose(outputFile);
  261.         outputFile = 0;
  262.     }
  263.     else if (matchesFlag(buffer,".>")) {
  264.         if (outputFile)
  265.         fclose(outputFile);
  266.         outputFile = fopen(buffer+2,"w");
  267.     }
  268.     else if (matchesFlag(buffer,".in "))
  269.         includeFile(buffer+4);
  270.     else if (matchesFlag(buffer,".po"))
  271.         suppressHeaders = 0;
  272.     else if (matchesFlag(buffer,".op"))
  273.         suppressHeaders = 1;
  274.         else if (setParam(buffer,".pn",&pageNumber)    ||
  275.                  setParam(buffer,".pl",&physicalLines) ||
  276.                  setParam(buffer,".ht",&headerLines1)  ||
  277.                  setParam(buffer,".hb",&headerLines2)  ||
  278.                  setParam(buffer,".ft",&footerLines1)  ||
  279.                  setParam(buffer,".fb",&footerLines2)  ||
  280.                  setParam(buffer,".ss",§Skip)      ||
  281.                  setParam(buffer,".mi",&minPage)       ||
  282.                  setParam(buffer,".mx",&maxPage))
  283.         continue;
  284.  
  285.         else if (setParam(buffer,".cc",&condClear)) {
  286.         if (sofarThisPage + condClear >= linesThisPage)
  287.         clearPage();
  288.     }
  289.  
  290.     else if (*buffer ||
  291.                  suppressHeaders ||
  292.                  (sofarThisPage>0 && sofarThisPage<linesThisPage)) {
  293.  
  294.         /* ignore blank lines if they occur at the top of the page with
  295.              * headers not suppressed
  296.          */
  297.  
  298.         if (sofarThisPage>=linesThisPage)    /* finish off any completed*/
  299.         clearPage();            /* page               */
  300.  
  301.         if (sofarThisPage==0)        /* maybe start a new page  */
  302.         startPage();
  303.         outputLine(buffer);        /* then print the line       */
  304.     }
  305.     }
  306.  
  307.     endDocument();
  308.     exit(0);
  309. }
  310.